package dobest.ray.com.jiongjiong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dobest.ray.corelibs.utils.ToastMgr;
import com.dobest.ray.corelibs.utils.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.bean.BindDeviceBean;
import dobest.ray.com.jiongjiong.utils.ACacheHelper;

/**
 * Created by Ray on 2016/3/15.
 * https://github.com/ray0807
 */
public class BindDevicesListAdapter extends BaseAdapter {
    private Context context;
    private List<BindDeviceBean> data = new ArrayList<>();

    public BindDevicesListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public BindDeviceBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_list_bind_devices, null);
        }
        TextView tv_device_name = ViewHolder.get(view, R.id.tv_device_name);
        TextView tv_device_mac = ViewHolder.get(view, R.id.tv_device_mac);
        TextView tv_device_unbind = ViewHolder.get(view, R.id.tv_device_unbind);
        tv_device_name.setText(data.get(position).deviceName);
        tv_device_mac.setText(data.get(position).deviceMac);
        tv_device_unbind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ACacheHelper.getInstance().clear(context, "BindDeviceBean");
                data.clear();
                notifyDataSetChanged();
                ToastMgr.show("解绑");
            }
        });
        return view;
    }


    public void addData(BindDeviceBean bean) {
        this.data.add(bean);
        notifyDataSetChanged();
    }

}
